<?php
	// Class: DataSource
	// 
	// Helper class to generate html table from db talbe
	//
	// Author: Arttu Valo
	// Date: 15.11.2006
	// Kaikki virheilmoitukset ja varoitukset esille
	error_reporting(E_ALL);
	
	require_once("Table.php");
	require_once("Tr.php");
	require_once("Td.php");
	require_once("Th.php");
	require_once("../Database/DBConnector.php");
	
	class TableGenerator
	{
		private $db;
		
		// Constructor		
		public function TableGenerator($dbType, $serverName, $userName, $passWord, $dbName)
		{
			$this->db = new DBConnector($dbType, $serverName, $userName, $passWord);
			$this->db->SelectDB($dbName);
		}
		
		// GererateTable
		// Generates HTML table from database table
		//
		// $tableName   - database table to generate html table from
		// $headerNames - assosiative array holding column headers
		// 
		// Returns string for the table
		public function GenerateTable($tableName, $headerNames)
		{
			$sql = "SELECT ".implode(", ", array_keys($headerNames))." FROM ".$tableName;
			
			$resultset = $this->db->Query($sql);
			
			$table = new Table();
			
			$table->setBorder("1");
			
			$headerRow = new Tr();
			
			foreach($headerNames as $key => $val)
			{
				$headerRow->addColumn(new Th($val));
			}
			
			$table->setColumnHeaders($headerRow);
			
			foreach($resultset as $result)
			{
				$tr = new Tr();
				foreach($headerNames as $key => $val)
				{
					$tr->addColumn(new Td($result->$key));
				}
				$table->addRow($tr);
			}
			
			return $table->getHtml();
		}
	}
/*
<html>
	<body>
		<?php
			$tg = new TableGenerator(DBConnector::DBC_MSSQL, "AEVALO", "opennh", "opennh", "OpenNeighbourhood");
			
			$headerNames = array("EventHeader" => "Event Header",
									"EventText" => "Event Text");
			
			echo $tg->GenerateTable("Events", $headerNames);
		?>
	</body>
</html>
*/
?>
